#Obtaining the full sample of all authors (do only once)#

library(igraph)
library(reshape)
setwd("/Users/Lorenzo/Google Drive/GenderDiff/RevisionRESTAT/Network codes")
source("getntwrks7auth.R")
source("netprod_7authorsbygender_soc.R")
source("analyzenetw.R")

data<-read.csv("networkfemaleapisoct.csv",header=TRUE)
colnames(data) <- c("articleid", "auth1","gender1","myear1","auth2", "gender2","myear2","auth3","gender3","myear3","auth4","gender4","myear4","auth5","gender5","myear5","auth6","gender6","myear6","auth7","gender7","myear7","year","npapers","prod","nauthors","prodd") 

# endyears and length to compute networks
# endyears and length to compute networks
ks <- 5
for (k in ks) {
  endyears <- (1970+k-1):1999
  for (endyear in endyears) {
    startyear <- endyear - k+1
    y <- subset(data,year>=startyear & year<=endyear)
    g <- getnetwork(startyear,endyear, data)
    y$auth7[is.na(y$auth7)]<--6 
    y$auth6[is.na(y$auth6)]<--5 
    y$auth5[is.na(y$auth5)]<--4 
    y$auth4[is.na(y$auth4)]<--3 
    y$auth3[is.na(y$auth3)]<--2 
    y$auth2[is.na(y$auth2)]<--1  
    V(g)$deg <- degree(g)
    netprodx<- sapply(V(g)[deg>0],netprodg,g,y,endyear)   
    V(g)[deg>0]$netprodmale <-netprodx[1,]    
    V(g)[deg>0]$netprodfemale <-netprodx[2,]   
    V(g)[deg>0]$papersmale <-netprodx[3,] 
    V(g)[deg>0]$papersfemale <-netprodx[4,] 
    V(g)[deg>0]$nmaleco <-netprodx[5,] 
    V(g)[deg>0]$nfemaleco <-netprodx[6,] 
    V(g)[deg>0]$tmale <-netprodx[7,] 
    V(g)[deg>0]$tfemale <-netprodx[8,] 
    V(g)$auth<-V(g)
    d<-data.frame(V(g)$auth,V(g)$deg,V(g)$netprodmale,V(g)$netprodfemale,V(g)$papersmale,V(g)$papersfemale,V(g)$nmaleco,V(g)$nfemaleco,V(g)$tmale,V(g)$tfemale)
    write.csv(d,file=paste("network", endyear, "_", k, "y.csv", sep=""),na=".")
  }
}

